<?php
declare (strict_types = 1);

namespace app\admin\controller\setting;

use app\admin\controller\AuthController;
use bbweb\services\UtilService as Util;
use app\admin\model\system\{SystemAdmin, SystemMenus, SystemRole as RoleModel};
use think\Request;

class SystemRole extends AuthController
{
    /**
     * 显示管理列表
     */
    public function index(Request $req)
    {
        if ($req->isPost()) {
            $array = RoleModel::roleAll();
            $this->success('ok','', $array, 0);
        }
        $sonAuth = SystemMenus::getSonAuth($req->controller(),$req->action(),array_keys($this->auth));
        $this->assign('sonAuth',$sonAuth);
        return $this->fetch();
    }

    /**
     * 权限分配
     */
    public function role(Request $req)
    {
        $id = $req->param('id/d');
        $group = RoleModel::find($id);
        if(empty($group)) {
            $this->error('参数错误！');
        }
        if($req->isPost()) {
            $item = Util::postMore([
                ['auth', [], '', 'roles']
            ], $req);
            $item['roles'] = implode(',', $item['roles']);
            $item['update_at'] = time();
            //dump($item);die;
            $res = RoleModel::where('id','=',$id)->update($item);
            if($res) {
                RoleModel::setPath();
                $this->success('操作成功!');
            } else {
                $this->error('操作失败');
            }
        }
        $menu_list = SystemMenus::ruleList();
        $this->assign('group',$group);
        $this->assign('menu_list',$menu_list);
        return $this->fetch();
    }

    /**
     * 新建
     */
    public function add(Request $req)
    {
        if ($req->isPost()) {
            $data = Util::postMore([
                'role_name',
                ['parent', 0],
                ['sys', 0]
            ], $req);
            if (!$data['role_name']) $this->error('请输入职位名称');
            $data['update_at'] = time();
            $res = RoleModel::create($data);
            if($res) {
                RoleModel::setPath();
                $this->success('操作成功!');
            } else {
                $this->error('操作失败');
            }
        }
        $pid = $req->param('pid/d',0);
        $parents = RoleModel::getSelect($pid);
        $this->assign('parents',$parents);
        $this->assign('pid',$pid);
        return $this->fetch();
    }

    /**
     * 修改
     */
    public function edit(Request $req)
    {
        $id = $req->param('id/d');
        $branch = RoleModel::find($id);
        if(empty($branch)) {
            $this->error('参数错误！','',-1);
        }
        if ($req->isPost()) {
            $item = [];
            if($req->param('change/d')) {
                $item['status'] = $req->param('status/d');
            } else {
                $item = Util::postMore([
                    'role_name',
                    ['parent', 0],
                    ['sys', 0]
                ], $req);
                if (!$item['role_name']) $this->error('请输入职位名称');
            }
            $item['update_at'] = time();
            $res = RoleModel::where('id','=',$id)->update($item);
            if($res) {
                RoleModel::setPath();
                $this->success('操作成功!');
            } else {
                $this->error('操作失败');
            }
        }
        $parents = RoleModel::getSelect(0, 0, $branch);
        $this->assign('branch',$branch);
        $this->assign('parents',$parents);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del(Request $req)
    {
        $id = $req->param('id/d');
        $group = RoleModel::where('id','=',$id)->find();
        if(empty($group)) {
            $this->error('参数错误！');
        } elseif(RoleModel::where('parent',$id)->find()) {
            $this->error('该部门下面有子部门不能删除！');
        } elseif(SystemAdmin::where('roleid',$id)->find()) {
            $this->error('该职位下面有员工不能删除！');
        } else {
            $bool = RoleModel::del($id);
            if($bool) {
                $this->success('操作成功！');
            } else {
                $this->error(RoleModel::getErrorInfo('删除失败,请稍候再试!'));
            }
        }
    }
}
